
//@ts-ignore
import * as global_way from "./lib/ways/global";
//@ts-ignore
import * as hints from "./lib/ways/hint";
// import * as filters from "./lib/ways/filter.js";
//@ts-ignore
import * as dates from "./lib/ways/date";
import * as verifys from "./lib/ways/verify";

//@ts-ignore
import skip from "./lib/ways/skip";
//@ts-ignore
import Http from './lib/ways/http'

import themes  from './static/theme.json'
import mitt from 'mitt'
import { reactive} from 'vue'
import type { App } from "vue";
import UsePageColor from './lib/use/UsePageColor'

type Thmems = {
	light:any,
	dark:any
}

interface optionsType {
	themes: Thmems;
	config: {
		baseUrl: string;
	};
	color: any;
	inputStyle: any;
	footStyle: any;
	btnStyle: any;
}

const plugin:any = {
	usePageColors:null,
	install(Vue:App, options:optionsType) {
		let prototype:any;
		let version = parseInt(Vue.version)
		if (version >= 3) {
			prototype = Vue.config.globalProperties
		} else {
			prototype = prototype
		}
		let systemInfo = uni.getSystemInfoSync()
		let themeKey:string = systemInfo.theme || 'light'
		let defaultColor:{
			main: string;
			minor: string;
			success: string;
			pattern: string;
			error: string;
			themeKey: string;
			theme: any,
			themes?:any
		} = {
			main: '#2172FE',
			minor: '#FEE420',
			success: '#4cd964',
			pattern: '#fff',
			error: '',
			themeKey,
			//@ts-ignore
			theme: themes[themeKey],
			themes: themes
		}
		if (options && options.themes) {
			defaultColor.themes = Object.assign(defaultColor.themes, options.themes)
			//@ts-ignore
			defaultColor.theme = Object.assign(defaultColor.theme, options.themes[themeKey])
		}
		// prototype.$color = new Proxy(defaultColor, {
		// 	get(target:any, key) {
		// 		return target[key]
		// 	},
		// 	set(target, key, value) {
		// 		console.log('被修改了')
		// 		target[key] = value
		// 		return true
		// 	}
		// })
		prototype.$color = reactive(defaultColor)
		plugin.usePageColors =reactive(prototype.$color)// 使用usePageColors 代替原来的$color
		prototype.$config = {
			baseUrl: ''
		}
		if (options && options.config) {
			prototype.$config = Object.assign(prototype.$config, options.config)
		}
		//@ts-ignore
		uni.$config = prototype.$config
		if (options && options.color) {
			prototype.$color = reactive(Object.assign(prototype.$color, options.color))
			plugin.usePageColors =prototype.$color// 使用usePageColors 代替原来的$color
			// prototype.$color = new Proxy(Object.assign(prototype.$color, options.color), {
			// 	get(target, key) {
			// 		return target[key]
			// 	},
			// 	set(target, key, value) {
			// 		console.log(key,value)
			// 		target[key] = value
			// 		return true
			// 	}
			// })
		}
		prototype.$inputStyle = Object.assign({}, options.inputStyle || {})
		prototype.$footStyle = Object.assign({}, options.footStyle || {})
		prototype.$btnStyle = Object.assign({}, options.btnStyle || {})
		Object.keys(global_way).forEach((key) => {
			prototype[`$${key}`] = global_way[key];
		});
		Object.keys(hints).forEach((key) => {
			prototype[`$${key}`] = hints[key];
		});
		// Object.keys(filters).forEach((key) => {
		// 	Vue.filter(key, filters[key]);
		// });
		Object.keys(skip).forEach((key) => {
			prototype[`$${key}`] = skip[key];
		});
		Object.keys(dates).forEach((key) => {
			prototype[`$${key}`] = dates[key];
		});
		Object.keys(verifys).forEach((key) => {
			//@ts-ignore
			prototype[`$${key}`] = verifys[key];
		});
		if (version >= 3) {
			prototype.$bus = mitt()
		} else {
			//@ts-ignore
			prototype.$bus = new Vue()
		}
		prototype.$http = new Http({
			...prototype.$config
		})
		//@ts-ignore
		uni.$http = prototype.$http
		/**
		 * $setp 设置场景 是暗黑还是光明模式
		 * @param {string} themeKey light 为光明 dark 为黑暗
		 */
		prototype.$setP = (themeKey:string) => {
			prototype.$color.themeKey = themeKey
			//@ts-ignore
			prototype.$color.theme = prototype.$color.themes[themeKey]
		}
		prototype.$setL = () => {

		}

	},
	usePageColor(){
		console.log(plugin.usePageColors,'plugin.usePageColors')
		return plugin.usePageColors
	}
};
export default plugin;
export const usePageColor = plugin.usePageColor